;【AHK2】                                                                                
; http://cenalulu.github.io/images/linux/vim_cheatsheet/classic.gif                    
; http://ahkv2.mallocx.com/Tutorial.htm                                                
; https://www.autohotkey.com/boards/viewforum.php?f=82&start=25                        
; Hotkey Resource: https://www.autohotkey.com/docs/v1/Hotkeys.htm#Symbols              
; Symbol Legend - [#, ^, !, +] are modifiers                                           
; # = Windows Key                                                                      
; ^ = Ctrl                                                                             
; ! = Alt                                                                              
; + = Shift                                                                            
                                                                                       
                                                                                       
;系统快捷键                                                                                 
;=====================================================================o                
;                    CapsLock Direction Navigator                                      
;-----------------------------------o---------------------------------o                
;      键\修饰    	?	     !(+)	      ^         ^!                                     
;        h         ←         选字        跳词      跳选词                                     
;        j         ↓         选字        跳词      跳选词                                     
;        k         ↑         选字        跳词      跳选词                                     
;        l         →         选字        跳词      跳选词                                     
;        u        PgUp       选字        跳词      跳选词                                     
;        p        PgDn       选字        跳词      跳选词                                     
;        ,        Home       选字        跳词      跳选词                                     
;        .        End        选字        跳词      跳选词                                     
;-----------------------------------o---------------------------------o                
; 自定义函数
SetTitleMatchMode "RegEx"
;sys_hotkey和doc_hotkey参考此例做成
app_hotkey(keyname, app_title, app_path) {
    activate(keyname)  ; This will be a closure due to app_title and app_path.
    {
        if WinExist(app_title)
            WinActivate
        else
            Run app_path
    }
    Hotkey keyname, activate
}

;方向键和翻页等键的定义
sys_hotkey(key){
    Switch{
        case (GetKeyState("control") = 0 and GetKeyState("Alt") = 0): Send      key
        case (GetKeyState("control") = 0 and GetKeyState("Alt") = 1): Send "^"  key 
        case (GetKeyState("control") = 1 and GetKeyState("Alt") = 0): Send "+"  key
        case (GetKeyState("control") = 1 and GetKeyState("Alt") = 1): Send "+^" key
        }
}
;加括号、加书名号的定义
doc_hotkey(key1, key2) {
Clipboard_temp := A_Clipboard
A_Clipboard := ""
Send '^x'
ClipWait
send key1 A_Clipboard key2
;send '{Right}'
A_Clipboard := Clipboard_temp
Clipboard_temp := ""

}

multiClick(key1,key2,key3,time){
static count:=0
if count>0{
    count++
    return
}
count:=1
settimer after, time
after(){
    if count=1{
        send key1
    }
    else if count=2{
        send key2
    }
    else if count=3{
        send key3
    }   
    else if count>3{
        tooltip count
    }
count:=0
}
}

;获得鼠标选中的文件清单充分利用clipboard来获得信息！CabinetWClass|TTOTAL_CMD|_cls_desk_|TFoxMainFrm.UnicodeClass
GetExplorerSelectedItem() {
    If WinActive("ahk_class CabinetWClass|TTOTAL_CMD") {
        Clipboard_temp := A_Clipboard
        A_Clipboard := ""
        Send "^c" ; this will copy a files' path to the clipboard
        ClipWait
        ; MsgBox A_Clipboard
        Path_to_Selection := A_Clipboard
        ;SplitPath FullFileName, &name, &dir, &ext, &name_no_ext, &drive
        A_Clipboard := Clipboard_temp
        Clipboard_temp := ""
        Return Trim(Path_to_Selection,"`n")
    }
}
;-----------------------------------o---------------------------------o

                                                                                      
; Win+N activates or launches Notepad.                                                 
app_hotkey "#n", "ahk_class Notepad", "notepad.exe"                                    
; Win+W activates or launches WordPad.                                                 
app_hotkey "#w", "ahk_class WordPadClass", "wordpad.exe" 
app_hotkey "#c", "ahk_class Notepad|WordPadClass", "calc.exe" 
#HotIf GetKeyState("CapsLock","P")
h::sys_hotkey("{left}")                                                   
j::sys_hotkey("{down}")                                                   
k::sys_hotkey("{up}")                                                     
l::sys_hotkey("{right}")                                                  
-::sys_hotkey("{PgUp}")                                                   
=::sys_hotkey("{PgDn}")                                                   
,::sys_hotkey("{Home}")                                                   
.::sys_hotkey("{End}") 
;单击删除一字，双击删除一词，三击删除一行
backspace::multiClick("{del}","^{del}","{home}+{end}{del}",-333)
s::{                                                                                      
    if GetKeyState("Ctrl") or GetKeyState("Alt") = 1                                   
        Send "^+{Tab}"                                                                 
    else                                                                               
        Send "^{Tab}"                                                                  
}                                                                                      
                                 
p:: {
    try LongPath:= GetExplorerSelectedItem()
    If LongPath {
        SplitPath LongPath, &name, &dir, &ext, &name_no_ext, &drive
        command1 := "cd " '"' dir '"' "{Enter}"
    }
    else
        command1 := ""
    if !GetKeyState("Alt") and !GetKeyState("Ctrl"){
        run "powershell " 
        WinWait "ahk_class ConsoleWindowClass"
        SendInput command1
    }  
    else {
        run "powershell Start-Process powershell -Verb runAs "
        WinWait "ahk_class ConsoleWindowClass"
        Sleep 1111
        SendInput command1
    }                                                      
    LongPath :="" 
    }                                                                                    
                                                                                       
;置顶当前窗口                                                                                
t::{                                                                                      
	WinSetAlwaysOnTop -1, "A"                                                          
	ToolTip "Caps+t to stick the window or cancel it."                                 
}
                                                                                 
;判断是否使用notepad2程序                                                                      
#n::{                                                                                      
    if WinExist("ahk_class Notepad2")                                                  
        WinActivate  ; Activate the window found above                                 
    else                                                                               
        Run "notepad"  ; Open a new Notepad window                                     
}                                                                                      

f::{
    filelist:= GetExplorerSelectedItem()
    if GetKeyState("Ctrl") or GetKeyState("Alt")
        triger := 1
    else
        triger := 0
    Loop parse, filelist, "`n", "`r"  ; Specifying `n prior to `r allows both Windows and Unix files to be parsed.
        {
        LongPath    :=  A_LoopField
        ;SplitPath LongPath, &name, &dir, &ext, &name_no_ext, &drive
        run  LongPath 
        }
    
    }
m::{
run "C:\programs\nyfedit8\retry.bat"
msgbox "please retry mybase!"
}
#HotIf

;相同快捷键在不同作用域下的情况。
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Obsidian 和 wps 快捷键
#HotIf winactive("ahk_exe Obsidian.exe|wps*.exe|word.exe") 
^!c::MsgBox "You pressed Control+Alt+C in Notepad."
^5:: send  "{end}~~{home}~~{end}" ;删除线
^6:: send "{home}{del}{del}{end}{backspace}{backspace}" ;取消删除线
CapsLock & 9::doc_hotkey("（", "）")
CapsLock & 0::doc_hotkey("《", "》")
CapsLock & 8::doc_hotkey("*", "*")
CapsLock & '::doc_hotkey("“", "”")
#HotIf

; VS Code 快捷键
#HotIf winactive("ahk_exe Code.exe") 
CapsLock & 9::doc_hotkey("(", ")")
CapsLock & 0::doc_hotkey("<", ">")
CapsLock & '::doc_hotkey('"','"')

#HotIf

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;initiating                                                                                    
ToolTip "CapsLock toolkit starts in 5s."                                               
SetTimer () => ToolTip(), -5000    

;TEST;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Hotkey "^!z", MyFunc

MyFunc(ThisHotkey)
{
    MsgBox "You pressed " ThisHotkey
}

;审阅模式;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
global RemapKeys := false
+CapsLock::{
global RemapKeys := !RemapKeys
if RemapKeys 
    ToolTip "审阅模式开."
else
    ToolTip "审阅模式关."
SetTimer () => ToolTip(), -2000    
}
#HotIf Remapkeys
h::sys_hotkey("^{left}")                                                   
j::sys_hotkey("{down 5}")                                                   
k::sys_hotkey("{up 5}")                                                     
l::sys_hotkey("^{right}")                                                  
-::sys_hotkey("{PgUp}")      
=::sys_hotkey("{PgDn}")                                                   
,::sys_hotkey("{Home}")                                                   
.::sys_hotkey("{End}") 
d::multiClick("{del}","^{del}","{home}+{end}{del}",-333)
#HotIf



